﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CodeDynamiksEngine;

using MESCodeFile;

namespace mes_tublay
{
    public partial class frmBPLSReports : Form
    {
        CodeScript script;
        CodeProcessor proc = new CodeProcessor(ThreadingMode.Normal);
        DataCache dat = new DataCache("myData");

        public frmBPLSReports()
        {
            InitializeComponent();
            InitializeFrameSliderForm();
        }

        void btnSearch_Click(object sender, EventArgs e)
        {
            searchItem();
        }

        void proc_ProcessCompleted(object sender, EventArgs e)
        {
            proc.SafeCall(this, delegate
            {
                if (proc.State == ProcessorFlag.Error ||
                    proc.State == ProcessorFlag.DataRdyErr ||
                    proc.State == ProcessorFlag.ReadyErr)
                {
                    MessageBox.Show(proc.GetProcessorMessage, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    proc.ClearErrorFlag();
                    return;
                }
                switch (proc.GetLastProcessName().ToString())
                {
                    case "Search":
                        proc.DataFetch(out dat);
                        //Set the active page before exporting
                        if (dat.GetPageSize() >= 1)
                        {
                            dat.SetActivePage(0);
                            grdBusinessPermit.DataSource = dat.ExportPage();
                         
                        }
                        break;
                }
            }); 
        }

        private void InitializeForm()
        {
            btnSearch.Click += new EventHandler(btnSearch_Click);
            proc.ProcessCompleted += new EventHandler(proc_ProcessCompleted);
            
            txtSearch.TextChanged += new EventHandler(txtSearch_TextChanged);

            loadDropDownList();
            searchItem();
        }

        
        void loadDropDownList()
        {
            cmbFilter.Items.Add("ALL");
            cmbFilter.Items.Add("APPROVED");
            cmbFilter.Items.Add("PENDING");
            cmbFilter.Items.Add("CLOSED");
            cmbFilter.Items.Add("DELINQUENT");
            cmbFilter.Items.Add("TRANSFER LOCATION");
            cmbFilter.Items.Add("TRANSFER OWNERSHIP");
        }

        
        void txtSearch_TextChanged(object sender, EventArgs e)
        {
            searchItem();
        }

        void searchItem()
        {
            //Check if the processor is executing script, if it is, return to the calling code.
            if (proc.State != ProcessorFlag.Ready && proc.State != ProcessorFlag.DataRdy) return;

            script = new CodeScript("Search");
            script.Add(CodeIns.SYS_RESET);
            script.Add(CodeIns.DBU_NEW_CORE);
            script.Add(CodeIns.DBU_CONNECT, Common.CFG.Read("DRIVER"), Common.CFG.Read("DB"), Common.CFG.Read("UID"), Common.CFG.Read("PWD"), Common.CFG.Read("SERVER"));
            script.Add(CodeIns.DBU_BEGIN_TRANSACTION);
            
            string filter = cmbFilter.Text;
            switch(filter)
            {
                case "ALL":
                    script.Add(CodeIns.DBU_QUERY, "SELECT t1.fReferenceNumber AS 'Reference Number',ffBusinessReferenceNumber AS 'Business Reference Number', ffApplicationNumber AS 'Application Number', ffBusinessName AS 'Business Name', ffStatus AS 'Business Status', fLastName AS 'Last Name', fFirstName AS 'First Name', fMiddleName AS 'Middle Name', fCompanyName AS 'Company Name' FROM tblBusinessOwnerProfile t1," +
                                                    "(SELECT fBusinessReferenceNumber AS ffBusinessReferenceNumber, fApplicationNumber AS ffApplicationNumber, fBusinessName AS ffBusinessName, fReferenceNumber, fBusinessStatus AS ffStatus FROM tblBusinessInformation) AS t2 " +
                                                    " WHERE (t1.fReferenceNumber = t2.fReferenceNumber) AND ((fLastName LIKE '%" + txtSearch.Text + "%') OR (fFirstName LIKE '%" + txtSearch.Text + "%') OR (fMiddleName LIKE '%" + txtSearch.Text + "%')" +
                                                    " OR (ffBusinessName LIKE '%" + txtSearch.Text + "%'))"
                                                    );
                    break;

                default:
                    script.Add(CodeIns.DBU_QUERY, "SELECT t1.fReferenceNumber AS 'Reference Number',ffBusinessReferenceNumber AS 'Business Reference Number', ffApplicationNumber AS 'Application Number', ffBusinessName AS 'Business Name', ffStatus AS 'Business Status', fLastName AS 'Last Name', fFirstName AS 'First Name', fMiddleName AS 'Middle Name', fCompanyName AS 'Company Name' FROM tblBusinessOwnerProfile t1," +
                                                    "(SELECT fBusinessReferenceNumber AS ffBusinessReferenceNumber, fApplicationNumber AS ffApplicationNumber, fBusinessName AS ffBusinessName, fReferenceNumber, fBusinessStatus AS ffStatus FROM tblBusinessInformation WHERE fBusinessStatus = '"+filter+"') AS t2 " +
                                                    " WHERE (t1.fReferenceNumber = t2.fReferenceNumber) AND ((fLastName LIKE '%" + txtSearch.Text + "%') OR (fFirstName LIKE '%" + txtSearch.Text + "%') OR (fMiddleName LIKE '%" + txtSearch.Text + "%')" +
                                                    " OR (ffBusinessName LIKE '%" + txtSearch.Text + "%'))"
                                                    );
                    break;
            }
            script.Add(CodeIns.DBU_END);
            proc.Run(script);
        }
    }
}
